<?php
namespace app\admin\controller;

class TermModel extends Auth
{
    public function __construct()
    {
        parent::__construct();
        $this->isLogin();
    }

    public function test()
    {
        $termModelModel=D("TermModel");
        return dump($termModelModel->getIdModelTags(1, 'term'));
    }
	
    public function index()
    {
        $termModelModel=D("TermModel");
        $termModelList = $termModelModel->getList();
        $this->assign('list', $termModelList);
        $active['model']['index']=1;
        $this->assign('active', $active);
        return $this->view->fetch();
    }
	
    public function add()
    {
	    if(IS_POST){
            $addModel = D("TermModel")->addModel();
            if($addModel){
			    return $this->success('添加成功', U('TermModel/index'));
		    }else{
			    return $this->error('添加失败', U('TermModel/index'));
		    }			
		} 
        $active['model']['add']=1;
        $this->assign('active', $active);
		return $this->view->fetch();
    }
	
    public function edit()
    {
        $id = I('get.id');
	    if(IS_POST){
            $setModel = D("TermModel")->setModel($id);
            if($setModel){
				manage_log_success();
			    return $this->success('修改成功', U('TermModel/index'));
		    }else{
				manage_log_error();
			    return $this->error('修改失败', U('TermModel/index'));
		    }			
		} 

        $termModelInfo = D("TermModel")->getIdModelInfo($id);
        $this->assign('info', $termModelInfo);
        $termTagsList = json_decode($termModelInfo['term_tags'], true);
        $this->assign('termTagsList', $termTagsList);
        $contentTagsList = json_decode($termModelInfo['content_tags'], true);
        $this->assign('contentTagsList', $contentTagsList);
        $active['model']['index']=1;
        $this->assign('active', $active);
		return $this->view->fetch();
    }
	
    public function del()
    {
        $reconfirm = $this->reconfirm('删除后无法恢复，是否确认删除？');
        if(!$reconfirm){
			 return $this->error('取消删除', U('TermModel/index'));
		}
           
        $id = I('get.id');
        $delModel = D("TermModel")->delModel($id);
        if($delModel){
			manage_log_success();
            return $this->success('删除成功', U('TermModel/index'));
        }else{
			manage_log_error();
            return $this->error('删除失败', U('TermModel/index'));
        }			
    }

    public function addTermTag()
    {
        $id  = I('get.id');
        $this->assign('formUrl', U('', ['id'=>$id]));
        return $this->addTag($id, 'term');
    }

    public function addContentTag()
    {
        $id  = I('get.id');
        $this->assign('formUrl', U('', ['id'=>$id]));
        return $this->addTag($id, 'content');
    }
	
    private function addTag($id, $for)
    {
        $for = strtolower($for);
	    if(IS_POST){
            $addTag = D("TermModel")->addTag($id, $for);
            if($addTag){
				manage_log_success();
			    return $this->success('添加成功', U('TermModel/edit', ['id'=>$id]));
		    }else{
				manage_log_error();
			    return $this->error('添加失败', U('TermModel/edit', ['id'=>$id]));
		    }
		}
		return $this->view->fetch('addtag');
    }

    public function delTermTag()
    {
        return $this->delTag('term');
    }

    public function delContentTag()
    {
        return $this->delTag('content');
    }

    private function delTag($for)
    {
        $id  = I('get.id');
        $tag = I('get.tag');
        $for = strtolower($for);
        $setTag = D("TermModel")->delTag($id, $tag, $for);
        if($setTag){
			manage_log_success();
            return $this->success('删除成功', U('TermModel/edit', ['id'=>$id]));
        }else{
			manage_log_error();
            return $this->error('删除失败', U('TermModel/edit', ['id'=>$id]));
        }
    }
}
